package com.system.seckillgoods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.bean.SeckillGoods;
import com.system.common.service.ISeckillGoodsService;
import com.system.common.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/seckillgoods")
public class SeckillGoodsController {

    @DubboReference
    private ISeckillGoodsService seckillGoodsService;

    @PostMapping("/add")
    public Result add(@RequestBody SeckillGoods seckillGoods){
        seckillGoodsService.addSeckillGoods(seckillGoods);
        return Result.ok("添加秒杀商品成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        seckillGoodsService.deleteSeckillGoods(id);
        return Result.ok("删除秒杀商品成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody SeckillGoods seckillGoods){
        seckillGoodsService.updateSeckillGoods(seckillGoods);
        return Result.ok("修改秒杀商品成功");
    }

    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestBody SeckillGoods seckillGoods){
        seckillGoodsService.updateStatus(seckillGoods.getId(), seckillGoods.getStatus());
        return Result.ok("修改秒杀商品状态成功");
    }

    @GetMapping("/list")
    public Result list(){
        return Result.ok("获取秒杀商品列表成功", seckillGoodsService.getSeckillGoodsList());
    }

    @GetMapping("/page")
    public Result page(SeckillGoods seckillGoods,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        return Result.ok("获取秒杀商品分页成功",
                seckillGoodsService.getPage(seckillGoods, new Page<>(pageNum, pageSize)));
    }

    /**
     * 根据活动ID获取该活动的所有秒杀商品
     */
    @GetMapping("/listByActivity/{activityId}")
    public Result listByActivity(@PathVariable Integer activityId){
        return Result.ok("获取活动秒杀商品成功",
                seckillGoodsService.getByActivityId(activityId));
    }


    @GetMapping("/getSeckillGoodsById/{id}")
    public Result getSeckillGoodsById(@PathVariable Integer id){
        return Result.ok("获取秒杀商品成功", seckillGoodsService.getSeckillGoodsById(id));
    }

    @GetMapping("/getSeckillGoodsByGoodsId/{goodsId}")
    public Result getSeckillGoodsByGoodsId(@PathVariable Integer goodsId){
        return Result.ok("获取秒杀商品成功", seckillGoodsService.getSeckillGoodsByGoodsId(goodsId));
    }

    /**
     * 检查某个秒杀活动是否在有效期内
     */
//    @GetMapping("/isActive/{id}")
//    public Result isActive(@PathVariable Integer id){
//        return Result.ok("校验秒杀状态成功", seckillGoodsService.isSeckillActive(id));
//    }

    /**
     * 扣减秒杀库存
     */
    @PutMapping("/reduceStock/{id}/{count}")
    public Result reduceStock(@PathVariable Integer id, @PathVariable Integer count){
        boolean success = seckillGoodsService.reduceStock(id, count);
        return success ? Result.ok("扣减库存成功") : Result.error("库存不足或扣减失败");
    }
}
